﻿using ACC.Extensions.Entities;
using ACC.Extensions.Utilities;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace ACC.Extensions.ClientBusiness
{
    public class ClientSettingHelper
    {
        public static LocalSetting ReadSetting()
        {
            string filePath = AppDomain.CurrentDomain.BaseDirectory + "_setting\\local.setting";
            if (!File.Exists(filePath))
            {
                return null;
            }
            try
            {
                using (StreamReader reader = new StreamReader(filePath))
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(LocalSetting));
                    return (LocalSetting)xmlSerializer.Deserialize(reader);
                }
            }
            catch (IOException ex)
            {
                Log4Helper.Error("Read setting file error: ", ex);
            }
            catch (Exception ex)
            {
                Log4Helper.Error("Read setting file error: ", ex);
            }

            return null;
        }

        public static List<PartTypePic> ReadPicList()
        {
            string filePath = AppDomain.CurrentDomain.BaseDirectory + "_setting\\parttype_pic_list.setting";
            if (!File.Exists(filePath))
            {
                return null;
            }
            try
            {
                using (StreamReader reader = new StreamReader(filePath))
                {
                    return JsonConvert.DeserializeObject<List<PartTypePic>>(reader.ReadToEnd());
                }
            }
            catch (IOException ex)
            {
                Log4Helper.Error("Read setting file error: ", ex);
            }
            catch (Exception ex)
            {
                Log4Helper.Error("Read setting file error: ", ex);
            }

            return null;
        }

        public static bool WritePicList(List<PartTypePic> list)
        {
            string settingFolder = AppDomain.CurrentDomain.BaseDirectory + "_setting";
            if (!Directory.Exists(settingFolder))
            {
                Directory.CreateDirectory(settingFolder);
            }

            string filePath = settingFolder + "\\parttype_pic_list.setting";

            try
            {
                using (StreamWriter writer = new StreamWriter(filePath))
                {
                    string s = JsonConvert.SerializeObject(list);
                    writer.Write(s);
                    return true;
                }
            }
            catch (IOException ex)
            {
                Log4Helper.Error("Save setting file error: ", ex);
            }
            catch (Exception ex)
            {
                Log4Helper.Error("Save setting file error: ", ex);
            }
            return false;
        }

        public static bool WriteSetting(LocalSetting settings)
        {
            string settingFolder = AppDomain.CurrentDomain.BaseDirectory + "_setting";
            if (!Directory.Exists(settingFolder))
            {
                Directory.CreateDirectory(settingFolder);
            }

            string filePath = settingFolder + "\\local.setting";

            try
            {
                using (StreamWriter writer = new StreamWriter(filePath))
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(LocalSetting));
                    xmlSerializer.Serialize(writer, settings);
                    return true;
                }
            }
            catch (IOException ex)
            {
                Log4Helper.Error("Save setting file error: ", ex);
            }
            catch (Exception ex)
            {
                Log4Helper.Error("Save setting file error: ", ex);
            }
            return false;
        }

        public static bool StorePic(string strSourcePath, string strFileName)
        {
            string strSettingFolder = AppDomain.CurrentDomain.BaseDirectory + "_pic";
            if (!Directory.Exists(strSettingFolder))
            {
                Directory.CreateDirectory(strSettingFolder);
            }
            if (!File.Exists(strSettingFolder + "\\" + strFileName))
            {

                System.IO.File.Copy(strSourcePath, strSettingFolder + "\\" + strFileName, true);
            }

            return false;
        }
    }
}
